package com.example.carRental.controller;

import com.example.carRental.entity.Car;
import com.example.carRental.service.ChartererService;
import com.example.carRental.utils.R;
import com.example.carRental.utils.RD;
import com.example.carRental.utils.RandomStr;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author 92307
 * @version 1.0
 * @time 2022/3/23
 * @Describe 出租方控制器
 */
@RestController
public class ChartererController {
    @Resource
    private ChartererService chartererService;
//    获得自身信息
//    @PreAuthorize("hasAuthority('charterer')")
    @GetMapping("/charterer/info")
    public RD getInfo(){
        System.out.println("cInfo进来了");
        System.out.println(chartererService.getInfo());
        return RD.success().putData("data",chartererService.getInfo());
    }

    //    图片上传地址
    private static final String filePath = "G:\\0project\\0springboot_projiect\\CarRental\\src\\main\\resources\\image";
    public static String getFilePath(){return filePath;}

    /**
     * @Description: 上传汽车车况图片
     * @Author: lpf
     * @Date: 2022/3/24
     */
//    @PreAuthorize("hasAuthority('charterer')")
    @PostMapping("/charterer/loadImage")
    @ResponseBody
    public R uploadFile(HttpServletRequest request, HttpServletRequest response, HttpSession session) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("image");//file是form-data中二进制字段对应的name
        String fileName = RandomStr.getRandomStr()+".jpg";//获取到上传文件的名字
        File dir = new File(filePath, fileName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //MultipartFile自带的解析方法
        multipartFile.transferTo(dir);
        //返回图片所在url
//        return "http://localhost:8080" + "/charterer/getImage/" + fileName;
        return R.success();
    }
    /**
     * @Description: 获得汽车车况图片
     * @Author: lpf
     * @Date: 2022/3/24
     */
//    @PreAuthorize("hasAuthority('charterer')")
    @GetMapping(value = "/charterer/getImage",produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getImage(@RequestParam("filePath") String filePath) throws Exception {
        File file = new File(ChartererController.filePath+"\\"+filePath);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        inputStream.close();
        return bytes;
    }
//    上传租车信息
    @PostMapping("/charterer/addCar")
    public R addCar(@RequestBody Car car){
        chartererService.addCar(car);
        return R.success();
    }

    /**
     * @Description: 查看已发布的车辆.
     * @Author: lpf
     * @Date: 2022/3/25
     */
    @PostMapping("/charterer/carList")
    public RD carList(){
        System.out.println("car被访问");
        List<Car> carList = chartererService.getCarList();
        System.out.println(carList);
        return RD.success().putData("data", carList).putData("total",carList.size());
    }


}
